package plugin.live.redis.list;

import java.io.Serializable;
import java.util.List;

import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

import lombok.Data;

/**
 * 对数据排序
 */
@Data
public class RedisList<T> implements Serializable{
	private static final long serialVersionUID = 3220065614302736625L;

	public void addAfter(T obj) {
		Cache cache = Redis.use();
		if (cache.llen(key) > sizeMax) {
			cache.lpop(key);// 移除第一个
		}
		cache.rpush(key, obj);// 新增到最后
	}

	public void addBefore(T obj) {
		Cache cache = Redis.use();
		if (cache.llen(key) > sizeMax) {
			cache.rpop(key);// 移除最后一个
		}
		cache.lpush(key, obj);// 新增到最前
	}

	public List<T> getList() {
		Cache cache = Redis.use();
		List<T> lrange = cache.lrange(key, 0, sizeMax);
		return lrange;
	}
	
	public long del() {
		Cache cache = Redis.use();
		return cache.del(key);
	}
	
	public T lpop() {
		Cache cache = Redis.use();
		return cache.lpop(key);
	}

	
	private String key = "";
	private int sizeMax = 100;
	
	public RedisList(String key, int sizeMax) {
		this.key = key;
		this.sizeMax = sizeMax;
	}

	
}
